# -*- coding: utf-8 -*-

# parse_votes.py

# Copyright (C) 2014 Fabian Wenzelmann
#
# This file is part of stura_voting.
#
# stura_voting is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# stura_voting is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with stura_voting. If not, see <http://www.gnu.org/licenses/>.
#

import sys

tab = '  '

def parseFile(_file):
  entries = []
  for line in _file:
    line = line.strip()
    if len(line) == 0 or line.startswith('#'):
      continue
    split = line.split('\t')
    if len(split) != 2:
      print('Ungültige Syntax in Datei')
      sys.exit(1)
    name, weight = split
    entries.append(tab + "{name:'%s'," % name + '\n' + tab + "weight:%s}" % weight)
  print(',\n'.join(entries))

def main():
  if len(sys.argv) % 2 != 1:
    print('Erwarte Liste von Bezeichnern [Variablenname] [Datei]')
    return
  i = 1
  while i < len(sys.argv):
    variable = sys.argv[i]
    _file = sys.argv[i + 1]
    i += 2
    try:
      f = open(_file, 'r')
      print('var %s = ' % variable)
      print('[')
      parseFile(f)
      print('];')
      f.close()
    except Exception as e:
      print('Datei %s kann nicht geöffnet werden:' % _file)
      print(e)

if __name__ == '__main__':
  main()
